package com.biz.primus.ms.oms.trans;

import com.biz.primus.model.oms.enums.CancelType;
import com.biz.primus.model.oms.enums.returns.AuditState;
import com.biz.primus.model.oms.enums.returns.ReturnState;
import com.biz.primus.model.oms.vo.OmsItemVo;
import com.biz.primus.model.oms.vo.OmsOrderReturnVo;
import com.biz.primus.model.oms.vo.returns.OmsReturnItemVo;
import com.biz.primus.model.oms.vo.returns.OmsReturnVo;
import com.biz.primus.ms.base.service.IdService;
import com.biz.primus.ms.oms.dao.po.OmsOrder;
import com.biz.primus.ms.oms.dao.po.OmsOrderItem;
import com.biz.primus.ms.oms.dao.po.returns.OmsReturn;
import com.biz.primus.ms.oms.dao.po.returns.OmsReturnItem;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yanweijin
 * @date 2017/11/17
 */
@Component
public class ReturnConverter {

	@Autowired
	private IdService idService;

	public OmsReturn vo2po(OmsOrderReturnVo vo, OmsOrder order){
		OmsReturn r=new OmsReturn();
		r.setId(idService.getNextId());
		r.setOrderId(order.getId());
		r.setUserId(vo.getUserId());
		r.setAuditState(AuditState.for_audit);
		r.setReturnState(ReturnState.begin);
		r.setCancelType(CancelType.none);
		r.setChannel(vo.getChannel());
		r.setReturnSource(vo.getReturnSource());
		r.setOrderSource(vo.getOrderSource());
		r.setNeedRefund(vo.isNeedRefund());
		r.setOriginData(vo);
		r.setReceiver(null);
		r.setReturnAmount(vo.getAmount());
		r.setRemark(vo.getRemark());
		r.setSelfRemark(vo.getSelfRemark());

		Map<String, OmsOrderItem> itemMap = Maps.uniqueIndex(order.getItems(), OmsOrderItem::getItemCode);
		List<OmsReturnItem> rItems = vo.getItems().stream().map(ivo->{
			OmsReturnItem item = new OmsReturnItem();
			OmsOrderItem oItem = itemMap.get(ivo.getItemCode());
			item.setQuantity(ivo.getQuantity());
			item.setFinalPrice(ivo.getFinalPrice());
			item.setItemId(oItem.getId());
			item.setProductCode(oItem.getProductCode());
			item.setProductName(oItem.getProductName());
			item.setReturns(r);
			item.setId(idService.getNextId());
			return item;
		}).collect(Collectors.toList());
		r.setItems(rItems);
		return r;
	}

	public OmsOrderReturnVo po2vo(OmsReturn po){
		OmsOrderReturnVo vo = new OmsOrderReturnVo();
		vo.setAmount(po.getReturnAmount());
		vo.setNeedRefund(po.isNeedRefund());
		vo.setUserId(po.getUserId());
		vo.setChannel(po.getChannel());
		vo.setCode(po.getReturnCode());
		vo.setDepot(po.getDepotCode());
		vo.setLiquidated(po.getRefundAmount());
		vo.setOrderSource(po.getOrderSource());
		vo.setReturnSource(po.getReturnSource());
		vo.setOriginOrderCode(po.getOrderCode());
		vo.setReceiver(po.getReceiver());
		vo.setReceiveTime(po.getReceiveTime());
		vo.setSelfRemark(po.getSelfRemark());
		vo.setRemark(po.getRemark());
		vo.setReturnState(po.getReturnState());
		return vo;
	}

	public List<OmsItemVo> poItems2voItems(List<OmsReturnItem> items, List<OmsOrderItem> orderItems) {
		ImmutableMap<Long, OmsOrderItem> itemMap = Maps.uniqueIndex(orderItems, OmsOrderItem::getId);
		//排除没有和orderItem对应的returnItem
		return items.stream().filter(i->itemMap.containsKey(i.getItemId())).map(i -> {
			OmsOrderItem oItem = itemMap.get(i.getItemId());
			return poItem2voItem(i, oItem);
		}).collect(Collectors.toList());
	}

	private OmsItemVo poItem2voItem(OmsReturnItem rItem, OmsOrderItem oItem){
		OmsItemVo vo = new OmsItemVo();
		vo.setWeight(oItem.getWeight());
		vo.setName(oItem.getProductName());
		vo.setCategory(oItem.getCategoryCode());
		vo.setQuantity(rItem.getQuantity());
		vo.setBrand(oItem.getBrandCode());
		vo.setBarcode(oItem.getBarcode());
		vo.setCode(oItem.getProductCode());
		vo.setDiscountAmount(oItem.getDiscountAmount());
		vo.setDiscountGrant(oItem.getDiscountGrant());
		vo.setDiscountPop(oItem.getDiscountPop());
		vo.setDiscountCoupon(oItem.getDiscountCoupon());
		vo.setFinalPrice(rItem.getFinalPrice());
		vo.setMarketPrice(oItem.getMarketPrice());
		vo.setPrice(oItem.getPrice());
		vo.setUsePoint(oItem.getUsePoint());
		vo.setItemCode(rItem.getId().toString());
		return vo;
	}


	public OmsReturnVo po2OmsReturnVo(OmsReturn omsReturn){
		OmsReturnVo omsReturnVo = new OmsReturnVo();
		omsReturnVo.setReturnSource(omsReturn.getReturnSource());
		omsReturnVo.setOrderSource(omsReturn.getOrderSource());
		omsReturnVo.setOrderCode(omsReturn.getOrderCode());
		omsReturnVo.setReturnAmount(omsReturn.getReturnAmount());
		omsReturnVo.setCreateTime(omsReturn.getCreateTimestamp().toLocalDateTime());

		List<OmsReturnItemVo> omsReturnItemVos = omsReturn.getItems().stream().map(req->{
			OmsReturnItemVo omsReturnItemVo =new OmsReturnItemVo();
			omsReturnItemVo.setProductCode(req.getProductCode());
			omsReturnItemVo.setProductName(req.getProductName());
			omsReturnItemVo.setQuantity(req.getQuantity());
			omsReturnItemVo.setFinalPrice(req.getFinalPrice());
			return omsReturnItemVo;
		}).collect(Collectors.toList());

		omsReturnVo.setOmsReturnItemVos(omsReturnItemVos);
		return omsReturnVo;
	}
}
